-- just simple treasure

local assets = {
	Asset("ANIM", "anim/musha_marks_spot.zip"),
	Asset("IMAGE", "images/inventoryimages/musha_treasure.tex"),
	Asset("ATLAS", "images/inventoryimages/musha_treasure.xml")
}

local prefabs = {
	"collapse_small"
}

local musha_treasure_list = {
	before = {
		{"musha_glowdust", 0.3, 4},
		{"musha_portion_e", 0.2, 2},
		{"goldnugget", 0.6, 10},
		{"bluegem", 0.35, 8},
		{"redgem", 0.35, 8},
		{"purplegem", 0.2, 4},
		{"orangegem", 0.1, 3},
		{"yellowgem", 0.1, 3},
		{"greengem", 0.1, 3},
		{"livinglog", 0.1, 8},
		{"gears", 0.4, 6},
		{"carrot", 0.1, 8},
		{"pumpkin", 0.1, 2},
		{"dragonfruit", 0.25, 1},
		{"watermelon", 0.3, 1},
		{"rocks", 0.5, 25},
		{"flint", 0.4, 45}
	},
	after = {
		{"musha_glowdust", 0.3, 4},
		{"musha_portion_e", 0.2, 2},
		{"goldnugget", 0.6, 10},
		{"bluegem", 0.1, 4},
		{"redgem", 0.1, 4},
		{"purplegem", 0.1, 4},
		{"orangegem", 0.1, 3},
		{"yellowgem", 0.1, 3},
		{"greengem", 0.1, 3},
		{"livinglog", 0.1, 8},
		{"gears", 0.25, 4},
		{"pumpkin", 0.3, 4},
		{"amulet", 0.1, 1},
		{"yellowamulet", 0.1, 1},
		{"orangeamulet", 0.1, 1},
		{"purpleamulet", 0.1, 1},
		{"blueamulet", 0.1, 1},
		{"greenamulet", 0.1, 1},
		{"rocks", 0.5, 60},
		{"flint", 0.4, 60}
	},
	common = {
		{"musha_glowdust", 0.6, 4},
		{"musha_portion_e", 0.6, 2},
		{"musha_apple", 0.5, 8}
	}
}

local guardians_list = {
	"spider",
	"spider_warrior",
	"tentacle",
	"ghost",
	"knight",
	"bishop",
	"terrorbeak",
	"leif_sparse"
}

local function dig_up(inst, chopper) --#TODO:随机可能性与物品
	SpawnPrefab("small_puff").Transform:SetPosition(inst:GetPosition():Get())
	SpawnPrefab("collapse_small").Transform:SetPosition(inst:GetPosition():Get())

	--danger
	if math.random() < 0.05 then
		SpawnPrefab("spider").Transform:SetPosition(inst:GetPosition():Get())
	elseif math.random() < 0.05 then
		SpawnPrefab("spider_warrior").Transform:SetPosition(inst:GetPosition():Get())
	elseif math.random() < 0.05 then
		SpawnPrefab("tentacle").Transform:SetPosition(inst:GetPosition():Get())
	elseif math.random() < 0.05 then
		SpawnPrefab("ghost").Transform:SetPosition(inst:GetPosition():Get())
	end

	local chest = SpawnPrefab("treasurechest")
	chest.Transform:SetPosition(inst:GetPosition():Get())

	if math.random() < 0.15 then --guardian
		SpawnPrefab("tentacle").Transform:SetPosition(inst:GetPosition():Get())
	elseif math.random() < 0.2 then
		SpawnPrefab("spider").Transform:SetPosition(inst:GetPosition():Get())
		SpawnPrefab("spider").Transform:SetPosition(inst:GetPosition():Get())
	elseif math.random() < 0.2 then
		SpawnPrefab("ghost").Transform:SetPosition(inst:GetPosition():Get())
	elseif math.random() < 0.25 then
		SpawnPrefab("spider_warrior").Transform:SetPosition(inst:GetPosition():Get())
		SpawnPrefab("spider_warrior").Transform:SetPosition(inst:GetPosition():Get())
	end

	--treasure box1
	if chest.components.container then
		if TheWorld.state.cycles < 85 then
			for i = 0, math.random(2) do
				for i = 0, math.random(2) do
					SpawnPrefab(guardians_list[math.random(#guardians_list)]).Transform:SetPosition(inst:GetPosition():Get())
				end
			end
			for k, v in pairs(musha_treasure_list.before) do
				for i = 1, v[3] do
					if math.random() < v[2] then
						chest.components.container:GiveItem(SpawnPrefab(v[1]))
					end
				end
			end
		else
			for i = 0, math.random(4) do
				for i = 0, math.random(3) do
					SpawnPrefab(guardians_list[math.random(#guardians_list)]).Transform:SetPosition(inst:GetPosition():Get())
				end
			end
			for k, v in pairs(musha_treasure_list.after) do
				for i = 1, v[3] do
					if math.random() < v[2] then
						chest.components.container:GiveItem(SpawnPrefab(v[1]))
					end
				end
			end
		end
		for k, v in pairs(musha_treasure_list.common) do
			for i = 1, v[3] do
				if math.random() < v[2] then
					chest.components.container:GiveItem(SpawnPrefab(v[1]))
				end
			end
		end
	end

	inst:Remove()
end

local function fn(Sim)
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon("musha_treasure.tex")
	--inst.MiniMapEntity:SetPriority(5)
	inst.MiniMapEntity:SetDrawOverFogOfWar(true)
	inst.MiniMapEntity:SetEnabled(true)

	inst.AnimState:SetBank("musha_marks_spot")
	inst.AnimState:SetBuild("musha_marks_spot")
	inst.AnimState:PlayAnimation("anim")

	inst.entity:SetPristine()
	if not TheWorld.ismastersim then
		return inst
	end

	inst:AddComponent("inspectable")

	inst:AddComponent("workable")
	inst.components.workable:SetWorkAction(ACTIONS.DIG)
	inst.components.workable:SetOnFinishCallback(dig_up)
	inst.components.workable:SetWorkLeft(1)

	SpawnPrefab("small_puff").Transform:SetPosition(inst:GetPosition():Get())
	SpawnPrefab("shovel").Transform:SetPosition(inst:GetPosition():Get())

	return inst
end

return Prefab("musha_treasure", fn, assets, prefabs)
